package com.aaa.three.service.impl;

import com.aaa.three.mapper.NewsMapper;
import com.aaa.three.pojo.domain.News;
import com.aaa.three.pojo.query.NewsQuery;
import com.aaa.three.pojo.vo.NewsVO;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @Author: whf
 * @Date: 2022/2/14 14:46
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public AjaxResult queryNews(NewsQuery newsQuery) {
        List<News> news = newsMapper.listNews(newsQuery);
        Long aLong = newsMapper.queryNewsTotal(newsQuery);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("currentData", news);
        map.put("totalCount", aLong);
        return AjaxResult.success(map);

    }

    @Override
    public AjaxResult addNews(NewsVO newsVO) {
        //  敏感词过滤
        String[] str1 = {"草","操","滚"};
        String[] str2 = {"SB","Sb","sB","sb","傻瓜",};
        String[] str3 = {"大法好","大傻瓜","真可爱","TMD"};

        for (String s : str1) {
            String result = newsVO.getTitle().replace(s,"*");
            String result1 = newsVO.getDetails().replace(s,"*");
            newsVO.setTitle(result);
            newsVO.setDetails(result1);
        }
        for (String s : str2) {
            String result = newsVO.getTitle().replace(s,"**");
            String result1 = newsVO.getDetails().replace(s,"**");
            newsVO.setTitle(result);
            newsVO.setDetails(result1);
        }
        for (String s : str3) {
            String result = newsVO.getTitle().replace(s,"***");
            String result1 = newsVO.getDetails().replace(s,"***");
            newsVO.setTitle(result);
            newsVO.setDetails(result1);
        }

        //  新闻添加
        int i = newsMapper.addNews(newsVO);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.fail("添加失败");
    }

    @Override
    public AjaxResult updateNews(NewsVO newsVO) {
        //  敏感词过滤
        String[] str1 = {"草","操","滚"};
        String[] str2 = {"SB","Sb","sB","sb","傻瓜",};
        String[] str3 = {"大法好","大傻瓜","真可爱","TMD"};

        for (String s : str1) {
            String result = newsVO.getTitle().replace(s,"*");
            String result1 = newsVO.getDetails().replace(s,"*");
            newsVO.setTitle(result);
            newsVO.setDetails(result1);
        }
        for (String s : str2) {
            String result = newsVO.getTitle().replace(s,"**");
            String result1 = newsVO.getDetails().replace(s,"**");
            newsVO.setTitle(result);
            newsVO.setDetails(result1);
        }
        for (String s : str3) {
            String result = newsVO.getTitle().replace(s,"***");
            String result1 = newsVO.getDetails().replace(s,"***");
            newsVO.setTitle(result);
            newsVO.setDetails(result1);
        }

        //  修改新闻
        int i = newsMapper.updateNews(newsVO);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.fail("修改失败");
    }

    @Override
    public AjaxResult deleteNews(Integer id) {
        int i = newsMapper.deleteNews(id);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.fail("删除失败");
    }

    @Override
    public AjaxResult getNewsById(Integer id) {
        NewsVO newsById = newsMapper.getNewsById(id);
        return AjaxResult.success(newsById);
    }

    @Override
    public AjaxResult updateQueryNews(Integer id) {
        News news = newsMapper.updateQueryNews(id);
        return AjaxResult.success(news);
    }

    @Override
    public AjaxResult updatePassCondition(Integer id) {
        int i = newsMapper.passCondition(id);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.fail("失败");
    }

    @Override
    public AjaxResult updateFailCondition(NewsVO newsVO) {
        int i = newsMapper.failCondition(newsVO);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.fail("失败");
    }

}
